<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>红黑树与 B 树对比</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #808080;
            border-bottom: 1px solid #3e3e3e;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 20px;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fadeInUp">
                红黑树与 B 树对比
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto animate-fadeInUp" style="animation-delay: 0.2s;">
                深入理解两种经典数据结构的设计哲学与应用场景
            </p>
            <div class="mt-10 flex justify-center space-x-8 animate-fadeInUp" style="animation-delay: 0.4s;">
                <div class="text-center">
                    <i class="fas fa-tree text-4xl mb-2"></i>
                    <p class="text-lg">红黑树</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-database text-4xl mb-2"></i>
                    <p class="text-lg">B 树</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-16">
        <!-- 题目描述 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="flex items-center mb-6">
                <div class="feature-icon bg-purple-100 text-purple-600">
                    <i class="fas fa-question-circle"></i>
                </div>
            </div>
            <h2 class="text-3xl font-bold mb-4 text-gray-800">题目描述</h2>
            <p class="text-lg text-gray-600 leading-relaxed">
                <span class="drop-cap">请</span>简要说明红黑树和 B 树的主要区别、各自的应用场景，以及它们在实际工程中的优缺点。
            </p>
        </div>

        <!-- 核心考点 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="flex items-center mb-6">
                <div class="feature-icon bg-blue-100 text-blue-600">
                    <i class="fas fa-lightbulb"></i>
                </div>
            </div>
            <h2 class="text-3xl font-bold mb-4 text-gray-800">核心考点</h2>
            <div class="flex flex-wrap gap-3">
                <span class="px-4 py-2 bg-gradient-to-r from-blue-500 to-purple-500 text-white rounded-full text-sm font-medium">红黑树</span>
                <span class="px-4 py-2 bg-gradient-to-r from-purple-500 to-pink-500 text-white rounded-full text-sm font-medium">B树</span>
                <span class="px-4 py-2 bg-gradient-to-r from-pink-500 to-red-500 text-white rounded-full text-sm font-medium">平衡树</span>
                <span class="px-4 py-2 bg-gradient-to-r from-red-500 to-orange-500 text-white rounded-full text-sm font-medium">磁盘IO</span>
            </div>
        </div>

        <!-- 数据结构对比图 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">数据结构关系图</h2>
            <div class="mermaid">
                graph TD
                    A[平衡查找树] --> B[二叉平衡树]
                    A --> C[多路平衡树]
                    B --> D[红黑树]
                    B --> E[AVL树]
                    C --> F[B树]
                    C --> G[B+树]
                    D --> H[内存数据结构]
                    F --> I[磁盘数据结构]
                    H --> J[TreeMap/map/set]
                    I --> K[数据库索引]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f56565,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- 解题思路 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <div class="flex items-center mb-6">
                <div class="feature-icon bg-green-100 text-green-600">
                    <i class="fas fa-brain"></i>
                </div>
            </div>
            <h2 class="text-3xl font-bold mb-6 text-gray-800">解题思路</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- 红黑树特点 -->
                <div class="bg-gradient-to-br from-red-50 to-pink-50 rounded-xl p-6 card-hover">
                    <h3 class="text-2xl font-bold mb-4 text-red-600 flex items-center">
                        <i class="fas fa-tree mr-3"></i>红黑树特点
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-red-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">二叉平衡查找树，适合内存中的有序数据结构</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-red-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">应用：Java的TreeMap、C++的map/set</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-red-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">节点度为2，每个节点最多两个子节点</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-red-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">查找、插入、删除时间复杂度：O(log n)</span>
                        </li>
                    </ul>
                </div>

                <!-- B树特点 -->
                <div class="bg-gradient-to-br from-green-50 to-blue-50 rounded-xl p-6 card-hover">
                    <h3 class="text-2xl font-bold mb-4 text-green-600 flex items-center">
                        <i class="fas fa-database mr-3"></i>B 树特点
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">多路平衡查找树，适合磁盘/数据库等大规模数据存储</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">应用：MySQL的索引、文件系统</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">节点度可大于2，适合批量读写</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">能有效减少磁盘IO次数</span>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- 复杂度分析 -->
            <div class="mt-8 bg-gray-50 rounded-xl p-6">
                <h4 class="text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-chart-line mr-2 text-purple-600"></i>复杂度分析
                </h4>
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="bg-white rounded-lg p-4 shadow">
                        <p class="text-gray-600 mb-1">时间复杂度</p>
                        <p class="text-2xl font-bold text-purple-600">O(log n)</p>
                    </div>
                    <div class="bg-white rounded-lg p-4 shadow">
                        <p class="text-gray-600 mb-1">空间复杂度</p>
                        <p class="text-2xl font-bold text-purple-600">O(n)</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 示例代码 -->
        <div class="bg-white rounded-2xl shadow-xl overflow-hidden mb-12">
            <div class="p-8">
                <div class="flex items-center mb-6">
                    <div class="feature-icon bg-yellow-100 text-yellow-600">
                        <i class="fas fa-code"></i>
                    </div>
                </div>
                <h2 class="text-3xl font-bold mb-6 text-gray-800">示例代码</h2>
                <p class="text-gray-600 mb-6">红黑树插入伪代码（TypeScript 风格）</p>
            </div>
            
            <div class="code-block">
                <div class="code-header">
                    <i class="fab fa-js-square mr-2"></i>TypeScript
                </div>
                <pre class="p-6 overflow-x-auto"><code class="text-sm">class tsRBNode {
  value: number;
  color: 'red' | 'black';
  left: tsRBNode | null;
  right: tsRBNode | null;
  // ...
}

function tsInsert(root: tsRBNode, value: number): tsRBNode {
  // 省略红黑树插入与调整逻辑
  return root;
}</code></pre>
            </div>
        </div>

        <!-- 应用场景对比 -->
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">应用场景对比</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="text-center p-6 bg-gradient-to-b from-purple-50 to-white rounded-xl card-hover">
                    <i class="fas fa-memory text-5xl text-purple-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-2">内存操作</h3>
                    <p class="text-gray-600">红黑树更适合频繁的内存操作场景</p>
                </div>
                <div class="text-center p-6 bg-gradient-to-b from-blue-50 to-white rounded-xl card-hover">